package com.saicmotor.springboot_demo.controller;

import com.saicmotor.springboot_demo.config.ValidateGroup;
import com.saicmotor.springboot_demo.model.UserRole;
import com.saicmotor.springboot_demo.model.UserRoleInfo;
import com.saicmotor.springboot_demo.model.json_pojo.JsonResult;
import com.saicmotor.springboot_demo.model.json_pojo.UserRoleUpdate;
import com.saicmotor.springboot_demo.service.CommonService;
import com.saicmotor.springboot_demo.service.UserRoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/user_role")
public class UserRoleController {
    @Autowired
    UserRoleService userRoleService;
    @Autowired
    CommonService commonService;

    @Autowired
    JsonResult jsonResult;

    @PostMapping("/add")
    public JsonResult insertUserRoleInfo(@RequestBody
                                             @Validated(ValidateGroup.UserRoleAdd.class) UserRoleInfo userRoleInfo) {
        if (userRoleService.checkRecord(userRoleInfo.getUserName(), userRoleInfo.getRoleName())) {
            return jsonResult.msg("This Record is EXISTS");
        } else if (!commonService.checkName("user", userRoleInfo.getUserName())) {
            return jsonResult.msg("userName is NOT EXIST");
        } else if (!commonService.checkName("role", userRoleInfo.getRoleName())) {
            return jsonResult.msg("roleName is NOT EXIST");
        } else {
            userRoleService.insertUserRoleInfo(userRoleInfo.getUserName(), userRoleInfo.getRoleName());
            return jsonResult.msg("Insert [" + userRoleInfo.getUserName() + "] plays [" + userRoleInfo.getRoleName() + "] Complete");
        }
    }

    @GetMapping("/all")
    public JsonResult getAll() {
        return jsonResult.data(userRoleService.getAll());
    }

    @GetMapping("/info")
    public JsonResult getAllInfo() {
        return jsonResult.data(userRoleService.getAllInfo());
    }

    @DeleteMapping("/delete/user")
    public JsonResult deleteByUserName(@RequestBody
                                           @Validated(ValidateGroup.UserRoleUserDelete.class) UserRoleInfo userRoleInfo) {
        if(!userRoleService.CheckByUserName(userRoleInfo.getUserName())){
            return jsonResult.msg("[" + userRoleInfo.getUserName() + "] Is Not Exist");
        }else {
            userRoleService.deleteByUserName(userRoleInfo.getUserName());
            return jsonResult.msg("Delete User [" + userRoleInfo.getUserName() + "] Complete");
        }
    }

    @DeleteMapping("/delete/role")
    public JsonResult deleteByRoleName(@RequestBody
                                           @Validated(ValidateGroup.UserRoleRoleDelete.class) UserRoleInfo userRoleInfo) {
        if(!userRoleService.CheckByRoleName(userRoleInfo.getRoleName())){
            return jsonResult.msg("[" + userRoleInfo.getRoleName() + "] Is Not Exist");
        }else {
        userRoleService.deleteByRoleName(userRoleInfo.getRoleName());
        return jsonResult.msg("Delete Role [" + userRoleInfo.getRoleName() + "] Complete");
        }
    }

    @PutMapping("/update/user")
    public JsonResult updateUser(@RequestBody
                                     @Validated(ValidateGroup.UpdateUser.class) UserRoleUpdate userRoleUpdate) {
        if(!userRoleService.checkRecord(userRoleUpdate.getOldUserName(), userRoleUpdate.getRoleName())){
            return jsonResult.msg("Cannot Found Old Record [" + userRoleUpdate.getOldUserName() + "] plays [" + userRoleUpdate.getRoleName() + "]");
        } else if (userRoleService.checkRecord(userRoleUpdate.getNewUserName(), userRoleUpdate.getRoleName())) {
            return jsonResult.msg("This Record [" + userRoleUpdate.getNewUserName() + "] plays [" + userRoleUpdate.getRoleName() + "] Record is EXISTS");
        } else if (!commonService.checkName("user", userRoleUpdate.getNewUserName())||
                !commonService.checkName("user", userRoleUpdate.getOldUserName())||
                !commonService.checkName("role", userRoleUpdate.getRoleName())) {
            return jsonResult.msg("Used Info is NOT EXIST");
        } else {
            userRoleService.updateUser(userRoleUpdate.getOldUserName(), userRoleUpdate.getNewUserName(), userRoleUpdate.getRoleName());
            return jsonResult.msg("Update [" + userRoleUpdate.getNewUserName() + "] plays [" + userRoleUpdate.getRoleName() + "] Complete");
        }
    }

    @PutMapping("/update/role")
    public JsonResult updateRole(@RequestBody
                                     @Validated(ValidateGroup.UpdateRole.class) UserRoleUpdate userRoleUpdate) {
        if(!userRoleService.checkRecord(userRoleUpdate.getUserName(), userRoleUpdate.getOldRoleName())){
            return jsonResult.msg("Cannot Found Old Record [" + userRoleUpdate.getUserName() + "] plays [" + userRoleUpdate.getOldRoleName() + "]");
        } else if (userRoleService.checkRecord(userRoleUpdate.getUserName(), userRoleUpdate.getNewRoleName())) {
            return jsonResult.msg("This Record [" + userRoleUpdate.getUserName() + "] plays [" + userRoleUpdate.getNewRoleName() + "] Record is EXISTS");
        } else if (!commonService.checkName("user", userRoleUpdate.getUserName())||
                !commonService.checkName("role", userRoleUpdate.getOldRoleName())||
                !commonService.checkName("role", userRoleUpdate.getNewRoleName())) {
            return jsonResult.msg("Used Info is NOT EXIST");
        } else {
            userRoleService.updateRole(userRoleUpdate.getOldRoleName(), userRoleUpdate.getNewRoleName(), userRoleUpdate.getUserName());
            return jsonResult.msg("Update [" + userRoleUpdate.getUserName() + "] plays [" + userRoleUpdate.getNewRoleName() + "] Complete");
        }
    }

    @PostMapping("/create")
    public JsonResult createUserRoleTable() {
        if (commonService.checkTable("user_role")) {
            return jsonResult.msg("[user_role] Table Exists");
        } else {
            userRoleService.createUserRoleTable("user_role");
            return jsonResult.msg("Create [user_role] Table Complete");
        }
    }
}
